package com.example.admin.datasupport.util;

import android.text.TextUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Admin on 2017/6/5.
 */

public class Utility {
    public static boolean handleProvinceResponse(String response){
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allProvinces=new JSONArray(response);
                for (int i=0;i<allProvinces.length();i++){
                    JSONObject provinceobjection=allProvinces.getJSONObject(i);
                    province province=new Province();
                    province.setProvinceName(provinceobjection.getString("name"));
                    province.setProvinceCode(provinceobjection.getInt("id"));
                    province.save();
                }
                return true;
            }catch (JSONException e){
                e.printStackTrace();
            }
        }
    return false;
}
    public static boolean handleCityResponse(String response,int provinceId){
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allCities=new JSONArray(response);
                for (int i=0;i<allCities.length();i++){
                    JSONObject cityobjection=allCities.getJSONObject(i);
                    City city=new City();
                    city.setCityName(Cityobjection.getString("name"));
                    city.setCityCode(Cityobjection.getInt("id"));
                    city.save();
                }
                return true;
            }catch (JSONException e){
                e.printStackTrace();
            }
        }
    return false;
}
    public static boolean handleCountyResponse(String response,int provinceId){
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allCounty=new JSONArray(response);
                for (int i=0;i<allCounty.length();i++){
                    JSONObject countyobjection=allCounty.getJSONObject(i);
                    County county=new County();
                    county.setCountyName(Countyobjection.getString("name"));
                    county.setCountyCode(Countyobjection.getInt("id"));
                    county.save();
                }
                return true;
            }catch (JSONException e){
                e.printStackTrace();
            }
        }
        return false;
    }

